package com.itdong.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itdong.vc.ResultVc;
import jdk.nashorn.internal.ir.debug.JSONWriter;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Slf4j
public class WebUtils {


    /**
     * 响应json类型的数据
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param resultVc 前后端统一信息格式
     * @throws Exception
     */
    public static void responseJsonMsg(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, ResultVc resultVc)  {
        //设置响应头
        try {
            httpServletResponse.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = httpServletResponse.getWriter();
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(resultVc);
            writer.write(json);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("WebUtils工具类出现异常");
        }
    }
}
